/***************************************************
> Copyright (C) 2024 ==QWZL== All rights reserved.
> File Name: income.c
> Author: xiaozhang
> Mail:1990614415@qq.com 
> Created Time: 2024年12月04日 星期三 09时40分28秒
***************************************************/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>

typedef struct Material
{
	int code;
	char name[32];
	char specification[32];
	char model[32];
	char unit[8];
	int quantity;
	char mytime[16];
	struct Material *next;
}MAT;
int main(int argc,const char* argv[])
{
	MAT *head = NULL;
	MAT *end = NULL;
	int t = 0;
	srand(time(NULL));
	char ch[5];
	MAT *temp = NULL;
	temp = (MAT *)malloc(sizeof(MAT));
	if(temp == NULL)
	{
		perror("malloc failed");
		return 1;
	}
	printf("请选择Y或N，是否有入库的材料");
	scanf("%s",ch);
	while(ch[0] == 'Y' || ch[0] == 'y')          //输入入库材料的相关信息
	{
		temp->code = rand()%1000000+1;			//生成唯一编码，六位随即数
	
		printf("请输入材料名称");
		scanf("%s",temp->name);
		printf("请输入材料规格");
		scanf("%s",temp->specification);
		printf("请输入材料型号");
		scanf("%s",temp->model);
		printf("请输入材料单位");
		scanf("%s",temp->unit);
		printf("请输入材料数量");
		scanf("%d",&temp->quantity);
		printf("请输入入库时间");
		scanf("%s",temp->mytime);
		printf("\n");
	
		if(head == NULL)
		{
			head = temp;
			end = temp;
		}
		else
		{
			end->next = temp;
			end = temp;
		}
		t++;
		printf("是否继续输入y/n");
		scanf("%s",ch);
		if(ch[0] == 'y' || ch[0] == 'Y')
		{
		temp = (MAT *)malloc(sizeof(MAT));
			if(temp == NULL)
			{
			perror("malloc failed");
			return 1;
			}
		}
	}
//	free(temp);		//释放最后一次分配空间的内存

	FILE *file = fopen("coming.csv","w");   //在excel表中显示出来
//	int MaterAmount = sizeof()
    int i = 0;
		if(file != NULL)
		{
			fprintf(file,"\xEF\xBB\xBF");
			fprintf(file,"%s,%s,%s,%s,%s,%s,%s","编码","名称","规格","型号","单位","数量","时间");
			MAT *p = head;
			while(p != NULL)
			{
			fprintf(file,"%d,%s,%s,%s,%s,%d,%s\n",temp->code,temp->name,temp->specification,temp->model,temp->unit,temp->quantity,temp->mytime);
			p = p->next;
			}
			fclose(file);                           //关闭文件
		}
		else
		{
		perror("fopen failed");
		return 1;
		}
	MAT *q = NULL;
	MAT *p = head;
	while(p != NULL)
	{
		q = p;
		free(q);
		p = q->next;
	}
	return 0;
}
